package com.fengwan.wastesort.db

import android.content.Context
import android.content.res.AssetManager
import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import com.fengwan.waste.util.writeTo
import java.io.File
import java.io.InputStream

/**
 * @author : Silence丶J
 * @time : 2019/7/17
 * @description :
 */
class DbManager constructor(private val context: Context){

    private fun unZip(dbPath: String, name: String) {
        if (!File(dbPath).exists()) {
            val inputStream: InputStream = context.assets.open(name)
            val file = File(dbPath)
            inputStream.use {
                it.writeTo(file.outputStream(), DEFAULT_BUFFER_SIZE)
            }
        }
    }

    fun getWasteCursor(dbPath: String, table: String, columns: ArrayList<String>): Cursor? {
        if (!File(dbPath).exists()) {unZip(dbPath, "waste.db")}
        val db = SQLiteDatabase.openOrCreateDatabase(dbPath, null)
        return db.query(table, null, null, null, null, null, null)
    }

}